﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Control.Home
{
    public partial class ColLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showmenu();
                lsvmuahang.DataSource = NewsService.News_GetByTop("6", "GroupNewId=9", "Id desc");
                lsvmuahang.DataBind();
                lsvnoibat.DataSource = NewsService.News_GetByTop("6", "Hotnews=1", "Id desc");
                lsvnoibat.DataBind();
                lsvbanchay.DataSource = ProductService.Product_GetByTop("6", "", "Views desc");
                lsvbanchay.DataBind();
                lsvquangcao.DataSource = AdvertiseService.Advertise_GetByPosition("2");
                lsvquangcao.DataBind();
            }
        }

        protected string Url(string GrId, string Id, string Name)
        {
            return "/News/" + GrId + "/" + Id + "/" + StringClass.NameToTag(Name) + ".aspx";
        }
        protected string Url1(string GrId, string Id, string Name)
        {
            return "/Pro/" + GrId + "/" + Id + "/" + StringClass.NameToTag(Name) + ".aspx";
        }
        protected void showmenu()
        {
            List<Data.GroupProduct> list = GroupProductService.GroupProduct_GetByAll();
            if (list.Count > 0)
            {
                this.ltr_menu.Text = "<ul id=\"left-menu\" class=\"menu\">";
                for (int i = 1; i < list.Count; i++)
                {

                    //có con
                    if (list[i - 1].Level.Length < list[i].Level.Length)
                    {
                        this.ltr_menu.Text += "<li class=\"icon last\"><a href=\"/GrProducts/" + list[i - 1].Id + "/"+StringClass.NameToTag(list[i - 1].Name)+".aspx\" target=\"_self\">" + list[i - 1].Name + "</a><ul style=\"visibility: hidden;\"> \n";
                    }

                    else if (list[i - 1].Level.Length == list[i].Level.Length)
                    {
                        this.ltr_menu.Text += "<li><a href=\"/GrProducts/" + list[i - 1].Id + "/" + StringClass.NameToTag(list[i - 1].Name) + ".aspx\" target=\"_self\">" + list[i - 1].Name + "</a></li>\n";
                    }
                    //không có con
                    else if (list[i - 1].Level.Length > list[i].Level.Length)
                    {
                        this.ltr_menu.Text += "<li><a href=\"/GrProducts/" + list[i - 1].Id + "/" + StringClass.NameToTag(list[i - 1].Name) + ".aspx\" target=\"_self\">" + list[i - 1].Name + "</a></li>\n";
                        int n = (list[i - 1].Level.Length - list[i].Level.Length) / 5;
                        this.ltr_menu.Text += Inma(n);
                    }
                }

                if (list[list.Count - 2].Level.Length < list[list.Count - 1].Level.Length)
                {

                    this.ltr_menu.Text += "<li><a href=\"/GrProducts/" + list[list.Count - 1].Id + "/" + StringClass.NameToTag(list[list.Count - 1].Name) + ".aspx\" target=\"_self\">" + list[list.Count - 1].Name + "</a></li>\n";
                    int s = (list[list.Count - 1].Level.Length - 5) / 5;
                    this.ltr_menu.Text += Inma(s);
                }
                else if (list[list.Count - 2].Level.Length == list[list.Count - 1].Level.Length)
                {

                    this.ltr_menu.Text += "<li><a href=\"/GrProducts/" + list[list.Count - 1].Id + "/" + StringClass.NameToTag(list[list.Count - 1].Name) + ".aspx\" target=\"_self\">" + list[list.Count - 1].Name + "</a></li>\n";
                    int s = (list[list.Count - 1].Level.Length - 5) / 5;
                    this.ltr_menu.Text += Inma(s);
                }
                else if (list[list.Count - 2].Level.Length > list[list.Count - 1].Level.Length)
                {
                    this.ltr_menu.Text += "<li><a href=\"/GrProducts/" + list[list.Count - 1].Id + "/" + StringClass.NameToTag(list[list.Count - 1].Name) + ".aspx\" target=\"_self\">" + list[list.Count - 1].Name + "</a></li>\n";
                }
                this.ltr_menu.Text += "</ul>";
            }

        }
        string Inma(int a)
        {
            string str = "";
            for (int j = 1; j <= a; j++)
            {
                str += "</ul></li>";
            }
            return str;
        }
    }
}